package com.ccp.dev.core.basic.base;

import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.response.ReturnCode;
import com.ccp.dev.core.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * web层通用控制器
 *
 * @author dfz
 */
public abstract class BaseController {
//TODO 添加两个方法：直接跳转页面；获取url里面的“/”

    /**
     *
     * 功能描述: 根据请求的路径，自动获取要跳转的页面地址
     *     例如访问地址： http://127.0.0.1:28882/platform/portal/testDemo/view/portal/list
     *     对应的页面文件地址为templates下的portal包中的list.html文件
     * @return: java.lang.String
     * @auther: dfz
     * @date: 2019/5/14 15:18
     */
    public String getAutoView() {
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        String requestURI = request.getRequestURI();
        // 处理RequestURI
        String contextPath = request.getContextPath();

        int cxtIndex = requestURI.indexOf(contextPath);
        if (cxtIndex != -1) {
            requestURI = requestURI.substring(cxtIndex + contextPath.length());
        }
        String[] paths = requestURI.split("[/]");
        int startIndex=paths.length;
        StringBuffer sb = new StringBuffer(requestURI.length());
        for(int i=0;i<paths.length;i++){
            String tmpPath=paths[i];
            if(StringUtil.isEmpty(tmpPath) || UrlConstants.VIEW_DISPATCH_PREFIX.contains(tmpPath)){
                continue;
            }
            sb.append("/"+tmpPath);
        }
        return sb.substring(1,sb.length());
    }

    /**
     *
     * 功能描述: 请求错误时返回指定的错误信息
     *
     * @param: err 错误信息
     * @return: com.ccp.dev.core.basic.response.ResultData
     * @auther: dfz
     * @date: 2019/5/14 15:23
     */
    public ResultData fail(String err) {
        if (StringUtils.isEmpty(err)) {
            err = ReturnCode.FAIL.getMsg();
        }
        return ResultData.error(ReturnCode.FAIL.getCode(), err);
    }

    /**
     * 功能描述: 返回默认的成功状态码和提示信息
     *
     * @return: com.ccp.dev.core.basic.response.ResultData
     * @auther: dfz
     * @date: 2019/4/30 13:29
     */
    public ResultData success() {
        return ResultData.success();
    }

    /**
     * 功能描述: 自定义操作成功后返回的提示内容
     *
     * @param: msg 指定的提示信息
     * @return: com.ccp.dev.core.basic.response.ResultData
     * @auther: dfz
     * @date: 2019/5/5 9:19
     */
    public ResultData success(String msg) {
        return ResultData.success(msg);
    }

    /**
     * 功能描述:自定义操作成功后返回的数据和提示内容
     *
     * @param: data 返回的数据
     * @param: msg  提示内容
     * @return: com.ccp.dev.core.basic.response.ResultData
     * @auther: dfz
     * @date: 2019/5/5 9:20
     */
    public ResultData success(Object data, String msg) {
        return ResultData.success(msg, data);
    }

    /**
     * 功能描述: 操作成功后返回的内容和总的数据条数
     *
     * @param: data 返回的数据
     * @param: count  总数据条数
     * @return: com.ccp.dev.core.basic.response.ResultData
     * @auther: dfz
     * @date: 2019/5/5 9:20
     */
    public ResultData success(Object data, int count) {
        return ResultData.success(data, count);
    }

    /**
     * 功能描述: 操作成功后返回最终的数据
     *
     * @param: data 返回的数据
     * @return: com.ccp.dev.core.basic.response.ResultData
     * @auther: dfz
     * @date: 2019/5/5 9:46
     */
    public ResultData success(Object data) {
        return ResultData.success(data);
    }
}
